package com.crm.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.crm.entity.Role;
import com.crm.repository.RoleRepository;
import com.crm.service.RoleService;
import com.crm.util.FenyeUtil;
@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleRepository repository;
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	@Override
	public FenyeUtil<Role> getRoleByFenYe(Role r, Integer page, Integer limit) {
		Pageable pageable=PageRequest.of(page-1, limit); 
		Page<Role> rlist = repository.findAll(get(r), pageable);
		FenyeUtil<Role> f=new FenyeUtil<Role>();
		f.setPage(page);//把page传入
		f.setLimit(limit);
		f.setData(rlist.getContent());
		f.setCount((int)rlist.getTotalElements());
		return f;
	}

public Specification<Role> get(Role r){
		
		return new Specification<Role>() {
			
			

			@Override
			public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				Predicate predicate=criteriaBuilder.conjunction();
				List<Expression<Boolean>> expressions=predicate.getExpressions();
				if(!StringUtils.isEmpty(r.getRoleName())) {
					expressions.add(criteriaBuilder.like(root.get("roleName"), "%"+r.getRoleName()+"%"));
				}
				return predicate;
			}
		};
		
	}

@Override
public String addRole(Role r) {
	try {
		r.setRoleCreateTime(sdf.format(new Date()));
		repository.saveAndFlush(r);
		
		return "添加成功";
	} catch (Exception e) {
		e.printStackTrace();
		return "添加失败";
	}
	
}

@Override
public String updateRole(Role r) {
	try {
		Optional<Role> role1=repository.findById(r.getRoleId());//取出原先role
		Role role=role1.get();
		role.setRoleExplain(r.getRoleExplain());
		role.setRoleName(r.getRoleName());
		role.setRoleLastUpdateTime(sdf.format(new Date()));		
		repository.saveAndFlush(role);		
		return "修改成功";
	} catch (Exception e) {
		e.printStackTrace();
		return "修改失败";
	}
}

@Override
public String deleteRole(Integer id) {
	try {
		repository.deleteById(id);;
		return "删除成功";
	} catch (Exception e) {
		e.printStackTrace();
		return "删除失败";
	}
}



@Override
public List<Role> getRoleByUserId(String id) {
	
	return repository.getRoleByUserId(id);
}

@Override
public String manageRole_Module(Integer rid, String mids) {
	if(mids==null||mids=="") {
		return "修改成功";
	}else {
		String[] mid=mids.split(",");
		System.out.println("mids:"+mids);
		
		repository.delRole_Module(rid);
		
		for(int i=0;i<mid.length;i++) {
			
			repository.addRole_Module(rid, Integer.valueOf(mid[i]));
		}
		return "修改成功";
	}
	
}
}
